package com.byxm.wlgw.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import com.byxm.wlgw.bean.KcxxBean;
import com.byxm.wlgw.bean.SpxxBean;
import com.byxm.wlgw.bean.SpxxpxBean;
import com.byxm.wlgw.common.DataBaseAccess;
import com.byxm.wlgw.common.Pages;
import com.byxm.wlgw.query.SpxxQuery;

public class SpxxService {
	//配置init
    private static final String BUNDLE_NAME = "init";
	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
	//图片服务器地址
	private String imgServiceUrl= RESOURCE_BUNDLE.getString("imgservice.server.url");
	
	SpxxQuery spxxQuery=new SpxxQuery("spxx");
	/**
	 * 通过SpxxBean 和page获取商品列表
	 * @param spxx
	 * @param pages
	 * @return
	 * @throws Exception
	 */
	public List<SpxxBean> getSpxx(SpxxBean spxx,Pages pages) throws Exception{
		List<SpxxBean> list=new ArrayList<SpxxBean>();
		list=this.getListfromDB(spxxQuery.getSpxxSql(spxx, pages));
	
		return list;
	}
	/**
	 * 通过商品商品信息id获取商品单条信息
	 * @return
	 */
	public SpxxBean getSpxxByspxxid(int spxxid){
		SpxxBean spxxBean=new SpxxBean();
		try {
			//获得单条商品信息
			spxxBean=this.getSpxxSql(spxxQuery.getSpxxBySpxxidSql(spxxid));
			String[] zstpStrArr=spxxBean.getSpzstp().split(",");
			String[] spmsStrArr=spxxBean.getSpms().split(",");
			List<String> lstZstpStr=new ArrayList<String>();
			List<String> lstSpmsStr=new ArrayList<String>();
			
			for(int i=0;i<zstpStrArr.length;i++){
				lstZstpStr.add(imgServiceUrl+zstpStrArr[i].trim());
			}
			for(int i=0;i<spmsStrArr.length;i++){
				lstSpmsStr.add(imgServiceUrl+spmsStrArr[i].trim());
			}
			//商品展示图片list
			spxxBean.setListZstp(lstZstpStr);
			//商品描述图片list
			spxxBean.setListSpms(lstSpmsStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return spxxBean;
	}
	
	
	
	public int getSpxxCount(SpxxBean spxx) throws Exception{
		DataBaseAccess dba=null;
		int count=0;
		try {
			dba = new DataBaseAccess();
			ResultSet rs = dba.executeQuery(spxxQuery.getSpxxCnt(spxx));
			if(rs.next()){
				count=rs.getInt("ct");
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		return count;
	}
	
	/**
	 * 保存商品信息  	(sphh,spbt,spms,spyj,spsj,fbrq,ppid,splbfid,splbsid,sptp,jgqjid,jhjg)
	 * @throws Exception 
	 */
	
	public boolean saveSpxx(SpxxBean bean) throws Exception{
		DataBaseAccess dba=null;
		PreparedStatement ps=null;
		int iFlag=0;
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr=df.format(new Date());
		try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(spxxQuery.getSaveSpxxSql());
			ps.setString(1, bean.getSphh());
			ps.setString(2, bean.getSpbt());
			ps.setString(3,bean.getSpms());
			ps.setFloat(4, bean.getSpyj());
			ps.setFloat(5,bean.getSpsj());
			ps.setString(6,dateStr);//当前时间
			ps.setInt(7, bean.getPpid());
			ps.setInt(8, bean.getSplbfid());
			ps.setInt(9, bean.getSplbsid());
			ps.setString(10, bean.getSptp());
			ps.setInt(11, bean.getJgqjid());
			ps.setFloat(12, bean.getJhjg());
			ps.setString(13, bean.getSpzstp());
			ps.setInt(14, bean.getKcsl());
			iFlag=ps.executeUpdate();
			dba.commit();
			ps.close();
			
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		if(iFlag>0){
			return true;
		}else{
			return false;
		}
		
	}
	/**
	 * 商品信息管理页面 
	 * @param spxxBean
	 * @param page
	 * @return
	 * @throws Exception
	 */
	public List<SpxxBean> findSpxxListgl(SpxxBean spxxBean,Pages page) throws Exception{
		String sql=spxxQuery.findSpxxSql(spxxBean,page);
		DataBaseAccess dba=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		List<SpxxBean> lstSpxxBean=new ArrayList<SpxxBean>();
		try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				SpxxBean spxx=new SpxxBean();
				spxx.setSpxxid(rs.getInt("spxxid"));
				spxx.setSphh(rs.getString("sphh"));
				spxx.setSpbt(rs.getString("spbt"));
				spxx.setSpms(rs.getString("spms"));
				spxx.setSpyj(rs.getFloat("spyj"));
				spxx.setSpsj(rs.getFloat("spsj"));
				spxx.setXssl(rs.getInt("xssl"));
				spxx.setPpid(rs.getInt("ppid"));
				spxx.setSplbfid(rs.getInt("splbfid"));//男鞋女鞋
				spxx.setSplbsid(rs.getInt("splbsid"));//功能 板鞋 篮球鞋
				spxx.setSpxszt(rs.getInt("spxszt"));//商品销售状态
				spxx.setSptp(imgServiceUrl+rs.getString("sptp"));//商品图片  在这里加
				spxx.setJhjg(rs.getFloat("jhjg"));
				spxx.setKcsl(rs.getInt("kcsl"));
				lstSpxxBean.add(spxx);
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		return lstSpxxBean;
		
	}
	
	/**
	 * 增加库存
	 * @throws Exception 
	 */
	public boolean updateKcsl(KcxxBean bean) throws Exception{
		DataBaseAccess dba=null;
		PreparedStatement ps=null;
		int iFlag=0;
		try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(spxxQuery.updateKcxxSql());
			ps.setInt(1, bean.getKcsl());
			ps.setInt(2, bean.getSpxxid());
			iFlag=ps.executeUpdate();
			dba.commit();
			ps.close();
			} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		if(iFlag>0){
			return true;
		}else{
			return false;
		}
		
		
		
	}
	
	/************私有方法区*********/
	
	/**
	 * 小判断
	 */
	private boolean xySpxxBean(SpxxBean bean){
		if(bean.getSplbfid()!=0||bean.getSplbsid()!=0||bean.getPpid()!=0||bean.getJgqjid()!=0||bean.getXhid()!=0||bean.getSearchString().trim()!=""){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 获取单条商品信息
	 * @throws Exception 
	 */
	private SpxxBean getSpxxSql(String sql) throws Exception{
		DataBaseAccess dba=null;
		SpxxBean spxx=new SpxxBean();
		try {
			dba = new DataBaseAccess();
			ResultSet rs = dba.executeQuery(sql);
			while (rs.next()) {	
				spxx.setSpxxid(rs.getInt("spxxid"));
				spxx.setSphh(rs.getString("sphh"));
				spxx.setSpbt(rs.getString("spbt"));
				spxx.setSpms(rs.getString("spms"));
				spxx.setSpyj(rs.getFloat("spyj"));
				spxx.setSpsj(rs.getFloat("spsj"));
				spxx.setXssl(rs.getInt("xssl"));
				spxx.setPpid(rs.getInt("ppid"));
				spxx.setSplbfid(rs.getInt("splbfid"));//男鞋女鞋
				spxx.setSplbsid(rs.getInt("splbsid"));//功能 板鞋 篮球鞋
				spxx.setSpxszt(rs.getInt("spxszt"));//商品销售状态
				spxx.setSptp(rs.getString("sptp"));//商品图片
				spxx.setSpppm(rs.getString("spppm"));//商品品牌名
				spxx.setSpks(rs.getString("spks"));
				spxx.setJhjg(rs.getFloat("jhjg"));
				spxx.setSpzstp(rs.getString("spzstp"));
				}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		return spxx;
	}
	private List<SpxxBean> getListfromDB(String sql) throws Exception{
		List<SpxxBean> list=new ArrayList<SpxxBean>();
		DataBaseAccess dba=null;
		try {
			dba = new DataBaseAccess();
			ResultSet rs = dba.executeQuery(sql);
			while (rs.next()) {	
				SpxxBean spxx=new SpxxBean();
				spxx.setSpxxid(rs.getInt("spxxid"));
				spxx.setSphh(rs.getString("sphh"));
				spxx.setSpbt(rs.getString("spbt"));
				spxx.setSpms(rs.getString("spms"));
				spxx.setSpyj(rs.getFloat("spyj"));
				spxx.setSpsj(rs.getFloat("spsj"));
				spxx.setXssl(rs.getInt("xssl"));
				spxx.setPpid(rs.getInt("ppid"));
				spxx.setSplbfid(rs.getInt("splbfid"));//男鞋女鞋
				spxx.setSplbsid(rs.getInt("splbsid"));//功能 板鞋 篮球鞋
				spxx.setSpxszt(rs.getInt("spxszt"));//商品销售状态
				spxx.setSptp(imgServiceUrl+rs.getString("sptp"));//商品图片  在这里加
			//	spxx.setKcsl(rs.getInt("kcsl"));
				list.add(spxx);
				}
			rs.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		return list;
	}
	private List<SpxxBean> getSpxxListFromRs(ResultSet rs){

		return null;
	}
	
}
